<?php
namespace app\index\controller;

use app\index\model\AccountListModel;
use app\index\model\ContractBianListModel;
use app\index\model\ContractBianModel;
use app\index\model\ContractBuModel;
use app\index\model\ContractCaiListModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractKuanListModel;
use app\index\model\ContractKuanModel;
use app\index\model\ContractNeiListModel;
use app\index\model\ContractNeiModel;
use app\index\model\ContractShiGongModel;
use app\index\model\ContractShiModel;
use app\index\model\ContractYangModel;
use app\index\model\DeptModel;
use app\index\model\DeviceJieModel;
use app\index\model\DeviceLinModel;
use app\index\model\EnginBudgetListModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginChanModel;
use app\index\model\EnginModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\IncomeEnginModel;
use app\index\model\LabourLingModel;
use app\index\model\LabourShenModel;
use app\index\model\PaymentEnginModel;
use app\index\model\PiaokaiModel;
use app\index\model\PiaoShouListModel;
use app\index\model\StuffJieModel;
use app\index\model\StuffRuListModel;
use app\index\model\StuffRuModel;
use app\index\model\SubbaoShenModel;
use app\index\model\UserModel;
use app\index\relevan\EnginRelevan;
use app\index\service\BudgetService;
use app\index\service\ContractService;
use app\index\service\EnginService;
use app\index\service\FlowService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\UserService;
use think\Db;
use think\Exception;
use think\Request;

class Contract extends Common
{
    public function list_contract_yang(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['yang.deleted_user','=', 0];
        $where[] = ['yang.comid','=',$this->userinfo['comid']];


        if(isset($param['yangname']) && !empty($param['yangname'])){
            $where[] = ['yang.yangname','like', '%'.$param['yangname'].'%'];
        }
        if(isset($param['yangshuo']) && !empty($param['yangshuo'])){
            $where[] = ['yang.yangshuo','like', '%'.$param['yangshuo'].'%'];
        }

        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['yang.state','=', $param['state']];
        }
        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['yang.year','like', '%'.$param['year'].'%'];
        }
        if(isset($param['caihetype']) && !empty($param['caihetype'])){
            $where[] = ['yang.caihetype','=', $param['caihetype']];
        }
        if(isset($param['caiheshu']) && !empty($param['caiheshu'])){
            $where[] = ['yang.caiheshu','=', $param['caiheshu']];
        }
        if(isset($param['caier']) && !empty($param['caier'])){
            $where[] = ['yang.caier','=', $param['caier']];
        }
        if(isset($param['myer']) && !empty($param['myer'])){
            $where[] = ['yang.myer','like', '%'.$param['myer'].'%'];
        }
        if(isset($param['qiandt']) && !empty($param['qiandt'])){
            $where[] = ['yang.qiandt','between', explode(' - ', $param['qiandt'])];
        }


        $chaninfo = (new UserService())->get_chaninfo_all();
        $data['caier'] = $chaninfo;
        $data['is_caier'] = isset($param['caier']) ? $param['caier'] : '';

        $data['state'] = (new ContractService())->get_contract_cai_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['caihetype'] = (new ContractService())->get_contract_caihetype();
        $data['is_caihetype'] = isset($param['caihetype']) ? $param['caihetype'] : '';

        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';

        $data['caiheshu'] = (new OptService())->get_base_info('engin_caiheshu');
        $data['is_caiheshu'] = isset($param['caiheshu']) ? $param['caiheshu'] : '';



        $data['data'] = ContractYangModel::alias('yang')
            ->field('yang.*')
            ->where($where)
            ->order('yang.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['caihetype_name'] = (new ContractService())->get_contract_caihetype_name($item['caihetype']);
                $item['caiheshu'] = (new OptService())->get_base_showname('engin_caiheshu', $item['caiheshu']);
                $item['caier_name'] = DeptModel::get_name_by_pk($item['caier']);
                $item['state_name'] = (new ContractService())->get_contract_cai_state_name_tag($item['id'], $item['state']);
                $item['created_user_name'] = UserModel::get_realname_by_pk($item['created_user']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_contract_yang(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['yangname'])){
                return outputJson(4000, '请输入模板标题');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();


            unset($param['token']);


            Db::startTrans();
            try {
                $id = ContractYangModel::insertGetId($param);

                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_yang', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_yang', $id, '采购合同模板');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $caiid = $request->get('caiid',0,'int');
            $caiinfo = ContractCaiModel::where(['id'=>$caiid])->find();
            if($caiinfo){
                $caiinfo['caier_info'] = (new UserService())->get_users_part_arr($caiinfo['caier'], 'dept');
                $data['caiinfo'] = $caiinfo;
            }

            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_cai_state();
            $data['caihetype'] = (new ContractService())->get_contract_caihetype();
            $data['caiheshu'] = (new OptService())->get_base_info('engin_caiheshu');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_yang(Request $request){
        if($request->isPost()){
            $param = $request->post();


            if(empty($param['yangname'])){
                return outputJson(4000, '请输入模板标题');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            unset($param['token']);

            Db::startTrans();
            try {
                ContractYangModel::update($param);

                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_yang', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_yang', $param['id'], '采购合同模板');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractYangModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['caier_info'] = (new UserService())->get_users_part_arr($data['data']['caier'], 'dept');

            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_cai_state();
            $data['caihetype'] = (new ContractService())->get_contract_caihetype();
            $data['caiheshu'] = (new OptService())->get_base_info('engin_caiheshu');
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_contract_yang(Request $request){
        $param = $request->post();
        $data = ContractYangModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            ContractYangModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'contract_yang', $data['id'], '采购合同模板');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_contract_yang(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = ContractYangModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['caihetype_name'] = (new ContractService())->get_contract_caihetype_name($data['data']['caihetype']);
        $data['data']['caiheshu'] = (new OptService())->get_base_showname('engin_caiheshu', $data['data']['caiheshu']);
        $data['data']['state_name'] = (new ContractService())->get_contract_cai_state_name($data['data']['id'], $data['data']['state']);
        $data['data']['caier_name'] = DeptModel::where(['id'=>$data['data']['caier']])->value('name');
        return outputJson(2000,lang('code_2000'), $data);
    }



    public function list_contract_nei(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['nei.deleted_user','=', 0];
        $where[] = ['nei.comid','=',$this->userinfo['comid']];
        $where[] = ['nei.isturn','=',1];
        $where[] = ['nei.status','in',[0,1]];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['nei.number|nei.name|nei.dutyer|nei.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['neihetype']) && !empty($param['neihetype'])){
            $where[] = ['nei.neihetype','=', $param['neihetype']];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['nei.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['nei.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['nei.year','=', $param['year']];
        }
        if(isset($param['dutyer']) && !empty($param['dutyer'])){
            $where[] = ['nei.dutyer','like', '%'.$param['dutyer'].'%'];
        }
        if(isset($param['dutytel']) && !empty($param['dutytel'])){
            $where[] = ['nei.dutytel','like', '%'.$param['dutytel'].'%'];
        }
        if(isset($param['canal']) && !empty($param['canal'])){
            $where[] = ['nei. canal','like', '%'.$param[' canal'].'%'];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['nei.state','=', $param['state']];
        }
        if(isset($param['qiandt']) && !empty($param['qiandt'])){
            $where[] = ['nei.qiandt','between', explode(' - ', $param['qiandt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['nei.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['settle']) && !empty($param['settle'])){
            $where[] = ['nei.settle','like', '%'.$param['settle'].'%'];
        }
        if(isset($param['clause']) && !empty($param['clause'])){
            $where[] = ['nei.clause','like', '%'.$param['clause'].'%'];
        }
        if(isset($param['viola']) && !empty($param['viola'])){
            $where[] = ['nei.viola','like', '%'.$param['viola'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['nei.status','=', $param['status']];
        }

        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['nei.chanid','=', $param['chanid']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['state'] = (new ContractService())->get_contract_nei_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';

        $data['data'] = ContractNeiModel::alias('nei')
            ->field('nei.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_nei', 'nei'))
            ->order('nei.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['neihetype_name'] = (new ContractService())->get_contract_neihetype_name($item['neihetype']);
                $item['state_name'] = (new ContractService())->get_contract_nei_state_name_tag($item['id'], $item['state']);
                $item['enginnums'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['status'=>1,'isturn'=>1,'neiid'=>$item['id']])->count();
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('contract_nei');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_contract_nei_enginnums(Request $request){
        $param = $request->post();
        get_page($param);

        $enginIdArr = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['status'=>1,'isturn'=>1,'neiid'=>$param['neiid']])->group('enginid')->column('enginid');



        $data['data'] = EnginModel::alias('e')
            ->field('e.*,xiang.realname as man_xiang_name')
            ->leftJoin('user xiang','xiang.id=e.man_xiang')
            ->where('e.id','in',$enginIdArr)
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['state_name_mob'] = (new EnginService())->get_engin_state_name($item['id'], $item['state']);
                $item['isting'] = $item['state'] == 3 ? true : false;

                $item['engintype'] = (new OptService())->get_base_showname('engin_engintype', $item['engintype']);
                $item['zhaotype'] = (new OptService())->get_base_showname('bid_bidding_type', $item['zhaotype']);
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_contract_nei(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['name'])){
                return outputJson(4000, '请输入协议名称');
            }
            if(empty($param['year'])){
                return outputJson(4000, '请选择协议年份');
            }
            if(empty($param['dutyer'])){
                return outputJson(4000, '请选择内包责任人');
            }
            if(empty($param['chanid'])){
                return outputJson(4000, '请选择所属单位');
            }
            if($param['state'] == ''){
                return outputJson(4000, '请选择合同状态');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = ContractNeiModel::insertGetId($param);
                (new OptService())->relation_file($files,'contract_nei',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_nei', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_nei', $id, '内包协议');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_nei_state();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_nei(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['name'])){
                return outputJson(4000, '请输入协议名称');
            }
            if(empty($param['year'])){
                return outputJson(4000, '请选择协议年份');
            }
            if(empty($param['dutyer'])){
                return outputJson(4000, '请选择内包责任人');
            }
            if(empty($param['chanid'])){
                return outputJson(4000, '请选择所属单位');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入费用设置');
            }


            foreach ($param as $kkk=>$vvv){
                if(strpos($kkk, 'feiname') !== false){
                    unset($param[$kkk]);
                }
            }

            Db::startTrans();
            try {
                ContractNeiModel::update($param);


                ContractNeiListModel::where(['neiid'=>$param['id']])->delete();
                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['neiid'] = $param['id'];
                        ContractNeiListModel::insert($item);
                    }
                }


                (new OptService())->relation_file($files,'contract_nei',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_nei', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_nei', $param['id'], '内包协议');

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractNeiModel::where(['id'=>$id])->find();
            if(!$data['data'] != 0){
                return outputJson(4000, lang('code_4000'));
            }

            $data['data']['chanid_info'] = (new UserService())->get_users_part_arr($data['data']['chanid'], 'dept');
            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_nei_state();
            $data['feitype'] = (new ContractService())->get_contract_nei_feitype();
            $data['feiname'] = (new OptService())->get_base_info('engin_nei_feiname');
            $data['files'] = (new OptService())->get_file('contract_nei', $data['data']['id']);



            $list = ContractNeiListModel::where(['neiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['feiname_name'] = (new OptService())->get_base_showname('engin_nei_feiname', $item['feiname']);
                $item['feitype_name'] = (new ContractService())->get_contract_nei_feitype_name($item['feitype']);
            }
            $data['list'] = $list;
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function qian_contract_nei(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['state'] == ''){
                return outputJson(4000, '请选择合同状态');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);
            Db::startTrans();
            try {
                ContractNeiModel::update($param);
                (new OptService())->relation_file($files,'contract_nei',$param['id']);
                (new OptService())->add_flow_log('签订', 'contract_nei', $param['id']);
                (new OptService())->add_log_list('签订', 'contract_nei', $param['id'], '内包协议');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractNeiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['state'] = (new ContractService())->get_contract_nei_state();
            $data['files'] = (new OptService())->get_file('contract_nei', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_contract_nei(Request $request){
        $param = $request->post();
        $data = ContractNeiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            ContractNeiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'contract_nei', $data['id'], '内包协议');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_contract_nei(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = ContractNeiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['neihetype_name'] = (new ContractService())->get_contract_neihetype_name($data['data']['neihetype']);
        $data['data']['state_name'] = (new ContractService())->get_contract_nei_state_name($data['data']['id'], $data['data']['state']);


        $list = ContractNeiListModel::where(['neiid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['feiname_name'] = (new OptService())->get_base_showname('engin_nei_feiname', $item['feiname']);
            $item['feitype_name'] = (new ContractService())->get_contract_nei_feitype_name($item['feitype']);
        }
        $data['list'] = $list;

        $data['data']['chanid_name'] = DeptModel::where(['id'=>$data['data']['chanid']])->value('name');
        $data['files'] = (new OptService())->get_file('contract_nei', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function kou_contract_nei(Request $request){
        if($request->isPost()){
            $param = $request->post();


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入费用设置');
            }

            Db::startTrans();
            try {
                ContractNeiListModel::where(['neiid'=>$param['id']])->delete();
                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['neiid'] = $param['id'];
                        ContractNeiListModel::insert($item);
                    }
                }
                (new OptService())->add_flow_log('扣费设置', 'contract_nei', $param['id']);
                (new OptService())->add_log_list('扣费设置', 'contract_nei', $param['id'], '内包协议');

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractNeiModel::where(['id'=>$id])->find();
            if(!$data['data'] != 0){
                return outputJson(4000, lang('code_4000'));
            }

            $data['feitype'] = (new ContractService())->get_contract_nei_feitype();
            $data['feiname'] = (new OptService())->get_base_info('engin_nei_feiname');

            $list = ContractNeiListModel::where(['neiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['feiname_name'] = (new OptService())->get_base_showname('engin_nei_feiname', $item['feiname']);
                $item['feitype_name'] = (new ContractService())->get_contract_nei_feitype_name($item['feitype']);
            }
            $data['list'] = $list;
            return outputJson(2000,lang('code_2000'),$data);
        }
    }


    public function list_contract_shi(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shi.deleted_user','=', 0];
        $where[] = ['shi.comid','=',$this->userinfo['comid']];
        $where[] = ['shi.isturn','=',1];
        $where[] = ['shi.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['shi.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.number|shi.name|shi.number|nei.name|shi.faer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['shi.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['shi.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['neiname']) && !empty($param['neiname'])){
            $where[] = ['nei.name','like', '%'.$param['neiname'].'%'];
        }
        if(isset($param['shihetype']) && !empty($param['shihetype'])){
            $where[] = ['shi.shihetype','=', $param['shihetype']];
        }
        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['shi.year','=', $param['year']];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['shi.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['chenger']) && !empty($param['chenger'])){
            $where[] = ['shi.chenger','=', $param['chenger']];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['shi.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['shi.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['shi.state','=', $param['state']];
        }
        if(isset($param['qiandt']) && !empty($param['qiandt'])){
            $where[] = ['shi.qiandt','between', explode(' - ', $param['qiandt'])];
        }
        if(isset($param['zhidt']) && !empty($param['zhidt'])){
            $where[] = ['shi.zhidt','between', explode(' - ', $param['zhidt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['shi.status','=', $param['status']];
        }


        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';



        $chaninfo = (new UserService())->get_chaninfo_all();
        $data['chenger'] = $chaninfo;
        $data['is_chenger'] = isset($param['chenger']) ? $param['chenger'] : '';

        $data['state'] = (new ContractService())->get_contract_shi_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';

        $data['shihetype'] = (new OptService())->get_base_info('engin_shihetype');
        $data['is_shihetype'] = isset($param['shihetype']) ? $param['shihetype'] : '';

        $data['data'] = ContractShiModel::alias('shi')
            ->field('shi.*,e.name as enginname,nei.name as neiname,e.business')
            ->join('engin e','e.id=shi.enginid')
            ->leftJoin('contract_nei nei','nei.id=shi.neiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_shi', 'shi'))
            ->order('shi.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['chenger_name'] = DeptModel::get_name_by_pk($item['chenger']);
                $item['shihetype'] = (new OptService())->get_base_showname('engin_shihetype', $item['shihetype']);
                $item['state_name'] = (new ContractService())->get_contract_shi_state_name_tag($item['id'], $item['state']);

                $item['shimoney'] = (new EnginService())->get_contract_shi_final_money($item['enginid']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('contract_shi');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_contract_shi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }

            $exist = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'enginid'=>$param['enginid']])
                ->where('status','in',[0,1])->find();
            if($exist){
                return outputJson(4000, '该项目下已存在施工合同，不能重复创建');
            }


            $engin = EnginModel::where(['id'=>$param['enginid']])->find();
            if($engin['business'] == 2){
                if(empty($param['neiid'])){
                    return outputJson(4000, '请选择内包协议');
                }
            }

            if(empty($param['name'])){
                return outputJson(4000, '请输入合同名称');
            }
            if(empty($param['shihetype'])){
                return outputJson(4000, '请选择合同类型');
            }
            if(empty($param['faer'])){
                return outputJson(4000, '请选择(发包方)甲方');
            }
            if(empty($param['chenger'])){
                return outputJson(4000, '请选择承包方(乙方)');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入合同金额');
            }
            if($param['state'] == ''){
                return outputJson(4000, '请选择合同状态');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);



            Db::startTrans();
            try {
                $id = ContractShiModel::insertGetId($param);

                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['shiid'] = $id;
                        ContractShiGongModel::insert($item);
                    }
                }

                (new EnginRelevan())->vate_engin_shimoney($param['enginid']);
                (new OptService())->relation_file($files,'contract_shi',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_shi', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_shi', $id, '施工合同');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_shi_state();
            $data['shihetype'] = (new OptService())->get_base_info('engin_shihetype');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_shi(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }

            $exist = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'enginid'=>$param['enginid']])
                ->where('status','in',[0,1])->where('id','neq',$param['id'])->find();
            if($exist){
                return outputJson(4000, '该项目下已存在施工合同，不能重复创建');
            }

            $engin = EnginModel::where(['id'=>$param['enginid']])->find();
            if($engin['business'] == 2){
                if(empty($param['neiid'])){
                    return outputJson(4000, '请选择内包协议');
                }
            }

            if(empty($param['name'])){
                return outputJson(4000, '请输入合同名称');
            }
            if(empty($param['shihetype'])){
                return outputJson(4000, '请选择合同类型');
            }
            if(empty($param['faer'])){
                return outputJson(4000, '请选择(发包方)甲方');
            }
            if(empty($param['chenger'])){
                return outputJson(4000, '请选择承包方(乙方)');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入合同金额');
            }


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');



            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            Db::startTrans();
            try {
                ContractShiModel::update($param);

                ContractShiGongModel::where(['shiid'=>$param['id']])->delete();
                if (count($list) > 0) {
                    foreach ($list as $item) {
                        $item['shiid'] = $param['id'];
                        ContractShiGongModel::insert($item);
                    }
                }


                (new EnginRelevan())->vate_engin_shimoney($param['enginid']);
                (new OptService())->relation_file($files,'contract_shi',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_shi', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_shi', $param['id'], '施工合同');

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractShiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $engininfo['business_name'] = $engininfo['business'] == 1 ? '自营' : '联营';
            $engininfo['chanid_name'] = DeptModel::get_name_by_pk($engininfo['chanid']);
            $engininfo['man_xiang_name'] = UserModel::get_realname_by_pk($engininfo['man_xiang']);
            $data['engininfo'] = $engininfo;

            $data['data']['neiid_name'] = ContractNeiModel::where(['id'=>$data['data']['neiid']])->value('name');
            $data['data']['chenger_info'] = (new UserService())->get_users_part_arr($data['data']['chenger'], 'dept');



            $list = ContractShiGongModel::where(['shiid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_shi_state();
            $data['zhilevel'] = (new ContractService())->get_contract_shi_zhilevel();
            $data['shihetype'] = (new OptService())->get_base_info('engin_shihetype');
            $data['files'] = (new OptService())->get_file('contract_shi', $data['data']['id']);

            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function qian_contract_shi(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['state'] == ''){
                return outputJson(4000, '请选择合同状态');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);
            Db::startTrans();
            try {
                ContractShiModel::update($param);
                (new OptService())->relation_file($files,'contract_shi',$param['id']);
                (new OptService())->add_flow_log('签订', 'contract_shi', $param['id']);
                (new OptService())->add_log_list('签订', 'contract_shi', $param['id'], '内包协议');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractShiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['state'] = (new ContractService())->get_contract_shi_state();
            $data['files'] = (new OptService())->get_file('contract_shi', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_contract_shi(Request $request){
        $param = $request->post();
        $data = ContractShiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            ContractShiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new EnginRelevan())->vate_engin_shimoney($data['enginid']);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'contract_shi', $data['id'], '施工合同');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_contract_shi(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = ContractShiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['shihemoney'] = (new EnginService())->get_contract_shi_final_money($data['data']['enginid']);


        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['data']['neiid_name'] = ContractNeiModel::where(['id'=>$data['data']['neiid']])->value('name');

        $data['data']['chenger_name'] = DeptModel::where(['id'=>$data['data']['chenger']])->value('name');

        $data['data']['shihetype'] = (new OptService())->get_base_showname('engin_shihetype', $data['data']['shihetype']);
        $data['data']['state_name'] = (new ContractService())->get_contract_shi_state_name($data['data']['id'], $data['data']['state']);

        $list = ContractBuModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1,'shiid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $gong = ContractShiGongModel::where(['shiid'=>$data['data']['id']])->select()->toArray();
        $data['gong'] = $gong;


        $data['data']['lei_chan'] = EnginChanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['enginid'=>$data['data']['enginid'],'status'=>1,'isturn'=>1])->sum('leijin');
        $data['data']['lei_piaokai'] = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['enginid'=>$data['data']['enginid'],'status'=>1,'isturn'=>1])
            ->where("dt is not null and dt != 0000-00-00 and dt != ''")->sum('money');
        $data['data']['lei_huikuan'] = IncomeEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['enginid'=>$data['data']['enginid']])->sum('money');

        $data['files'] = (new OptService())->get_file('contract_shi', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_contract_bu(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bu.deleted_user','=', 0];
        $where[] = ['bu.comid','=',$this->userinfo['comid']];
        $where[] = ['bu.isturn','=',1];
        $where[] = ['bu.status','in',[0,1]];
        $where[] = ['bu.shiid','=',$param['shiid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bu.name|bu.number|bu.money|bu.qianer','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['bu.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bu.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['bu.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['qianer']) && !empty($param['qianer'])){
            $where[] = ['bu.qianer','like', '%'.$param['qianer'].'%'];
        }
        if(isset($param['qiandt']) && !empty($param['qiandt'])){
            $where[] = ['bu.qiandt','between', explode(' - ', $param['qiandt'])];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bu.status','=', $param['status']];
        }

        $data['data'] = ContractBuModel::alias('bu')
            ->field('bu.*,e.name as enginname,shi.name as shiname')
            ->join('contract_shi shi','shi.id=bu.shiid')
            ->join('engin e','e.id=shi.enginid')
            ->where($where)
            ->order('bu.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();


        $data['shiinfo'] = ContractShiModel::where(['id'=>$param['shiid']])->find();
        $data['butauth'] = (new MenuService())->get_butauth('contract_bu', 1);
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_contract_bu(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['name'])){
                return outputJson(4000, '请输入补充合同名称');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入合同金额');
            }

            $param['enginid'] = ContractShiModel::where(['id'=>$param['shiid']])->value('enginid');


            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                $id = ContractBuModel::insertGetId($param);
                (new EnginRelevan())->vate_engin_shimoney($param['enginid']);
                (new OptService())->relation_file($files,'contract_bu',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_bu', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_bu', $id, '补充合同');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $shiid = $request->param('shiid', 0, 'int');
            $shiinfo = ContractShiModel::where(['id'=>$shiid])->find();
            $shiinfo['chenger'] = DeptModel::where(['id'=>$shiinfo['chenger']])->value('name');

            $data['shiinfo'] = $shiinfo;
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_bu(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = ContractBuModel::where(['id'=>$param['id']])->find();

            if(empty($param['name'])){
                return outputJson(4000, '请输入补充合同名称');
            }
            if(empty($param['money'])){
                return outputJson(4000, '请输入合同金额');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            Db::startTrans();
            try {
                ContractBuModel::update($param);
                (new EnginRelevan())->vate_engin_shimoney($old['enginid']);
                (new OptService())->relation_file($files,'contract_bu',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_bu', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_bu', $param['id'], '补充合同');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractBuModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $shiinfo = ContractShiModel::where(['id'=>$data['data']['shiid']])->find();
            $shiinfo['chenger'] = DeptModel::where(['id'=>$shiinfo['chenger']])->value('name');
            $data['shiinfo'] = $shiinfo;

            $data['files'] = (new OptService())->get_file('contract_bu', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_contract_bu(Request $request){
        $param = $request->post();
        $data = ContractBuModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            ContractBuModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new EnginRelevan())->vate_engin_shimoney($data['enginid']);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'contract_bu', $data['id'], '补充合同');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_contract_bu(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = ContractBuModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $shiinfo = ContractShiModel::where(['id'=>$data['data']['shiid']])->find();
        $shiinfo['chenger'] = DeptModel::where(['id'=>$shiinfo['chenger']])->value('name');
        $data['shiinfo'] = $shiinfo;

        $data['files'] = (new OptService())->get_file('contract_bu', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_contract_cai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cai.deleted_user','=', 0];
        $where[] = ['cai.comid','=',$this->userinfo['comid']];
        $where[] = ['cai.isturn','=',1];
        $where[] = ['cai.status','in',[0,1]];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['cai.enginid','=', $param['enginid']];
        }
        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name|e.number|cai.name|cai.number|cai.gonger','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['state']) && !empty($param['state'])){
            $where[] = ['cai.state','=', $param['state']];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['cai.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['cai.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['caihetype']) && !empty($param['caihetype'])){
            $where[] = ['cai.caihetype','=', $param['caihetype']];
        }
        if(isset($param['caiheshu']) && !empty($param['caiheshu'])){
            $where[] = ['cai.caiheshu','=', $param['caiheshu']];
        }
        if(isset($param['year']) && !empty($param['year'])){
            $where[] = ['cai.year','like', '%'.$param['year'].'%'];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['caier']) && !empty($param['caier'])){
            $where[] = ['cai.caier','=', $param['caier']];
        }
        if(isset($param['gonguser']) && !empty($param['gonguser'])){
            $where[] = ['cai.gonguser','like', '%'.$param['gonguser'].'%'];
        }
        if(isset($param['myer']) && !empty($param['myer'])){
            $where[] = ['cai.myer','like', '%'.$param['myer'].'%'];
        }
        if(isset($param['gongtel']) && !empty($param['gongtel'])){
            $where[] = ['cai.gongtel','like', '%'.$param['gongtel'].'%'];
        }
        if(isset($param['qiandt']) && !empty($param['qiandt'])){
            $where[] = ['cai.qiandt','between', explode(' - ', $param['qiandt'])];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['cai.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['rate']) && !empty($param['rate'])){
            $where[] = ['cai.rate','like', '%'.$param['rate'].'%'];
        }
        if(isset($param['ratefee']) && !empty($param['ratefee'])){
            $where[] = ['cai.ratefee','like', '%'.$param['ratefee'].'%'];
        }
        if(isset($param['noratefee']) && !empty($param['noratefee'])){
            $where[] = ['cai.noratefee','like', '%'.$param['noratefee'].'%'];
        }
        if(isset($param['startdt']) && !empty($param['startdt'])){
            $where[] = ['cai.startdt','between', explode(' - ', $param['startdt'])];
        }
        if(isset($param['enddt']) && !empty($param['enddt'])){
            $where[] = ['cai.enddt','between', explode(' - ', $param['enddt'])];
        }
        if(isset($param['zhirate']) && !empty($param['zhirate'])){
            $where[] = ['cai.zhirate','like', '%'.$param['zhirate'].'%'];
        }
        if(isset($param['zhifee']) && !empty($param['zhifee'])){
            $where[] = ['cai.zhifee','like', '%'.$param['zhifee'].'%'];
        }


        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['cai.status','=', $param['status']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $chaninfo = (new UserService())->get_chaninfo_all();
        $data['caier'] = $chaninfo;
        $data['is_caier'] = isset($param['caier']) ? $param['caier'] : '';

        $data['state'] = (new ContractService())->get_contract_cai_state();
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['caihetype'] = (new ContractService())->get_contract_caihetype();
        $data['is_caihetype'] = isset($param['caihetype']) ? $param['caihetype'] : '';

        $data['year'] = (new ContractService())->get_contract_yeararr();
        $data['is_year'] = isset($param['year']) ? $param['year'] : '';

        $data['caiheshu'] = (new OptService())->get_base_info('engin_caiheshu');
        $data['is_caiheshu'] = isset($param['caiheshu']) ? $param['caiheshu'] : '';



        $data['data'] = ContractCaiModel::alias('cai')
            ->field('cai.*,e.name as enginname')
            ->join('engin e','e.id=cai.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_cai', 'cai'))
            ->order('cai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['caihetype_name'] = (new ContractService())->get_contract_caihetype_name($item['caihetype']);
                $item['caiheshu'] = (new OptService())->get_base_showname('engin_caiheshu', $item['caiheshu']);
                $item['caier_name'] = DeptModel::get_name_by_pk($item['caier']);
                $item['state_name'] = (new ContractService())->get_contract_cai_state_name_tag($item['id'], $item['state']);

                $item['bianfee'] = ContractBianModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['isturn'=>1,'status'=>1,'caiid'=>$item['id']])->sum('bianfee');
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('contract_cai');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_contract_cai(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if($param['state'] == ''){
                return outputJson(4000, '请选择合同状态');
            }
            if(empty($param['name'])){
                return outputJson(4000, '请输入合同名称');
            }
            if(empty($param['year'])){
                return outputJson(4000, '请选择合同年份');
            }
            if(empty($param['caihetype'])){
                return outputJson(4000, '请选择合同类型');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请选择供应方(乙方)');
            }
            if(empty($param['caier'])){
                return outputJson(4000, '请选择采购方(甲方)');
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入合同明细');
            }







            Db::startTrans();
            try {
                $id = ContractCaiModel::insertGetId($param);

                $isbudget = EnginBudgetModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$param['enginid']])->find();
                foreach($list as $key=>$vv){
                    if($isbudget){
                        $bglist = EnginBudgetListModel::where(['id'=>$vv['listid']])->find();
                        $zhannum = ContractCaiListModel::alias('list')
                            ->join('contract_cai cai','cai.id = list.caiid')
                            ->where(['cai.deleted_user'=>0,'cai.isturn'=>1])
                            ->where('cai.status','in',[0,1])
                            ->where(['list.listid'=>$vv['listid']])
                            ->sum('list.num');
                        $shengnum = $bglist['num'] - $zhannum;
                        if($vv['num'] > $shengnum){
                            throw new Exception("第".$key."行采购数量已超预算剩余量");
                        }
                    }
                }

                foreach($list as $vv){
                    $vv['caiid'] = $id;
                    ContractCaiListModel::insert($vv);
                }

                (new OptService())->relation_file($files,'contract_cai',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_cai', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_cai', $id, '采购合同');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_cai_state();
            $data['caihetype'] = (new ContractService())->get_contract_caihetype();
            $data['caiheshu'] = (new OptService())->get_base_info('engin_caiheshu');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_cai(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = ContractCaiModel::where(['id'=>$param['id']])->find();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }

            if(empty($param['name'])){
                return outputJson(4000, '请输入合同名称');
            }
            if(empty($param['year'])){
                return outputJson(4000, '请选择合同年份');
            }
            if(empty($param['caihetype'])){
                return outputJson(4000, '请选择合同类型');
            }
            if(empty($param['gonger'])){
                return outputJson(4000, '请选择供应方(乙方)');
            }
            if(empty($param['caier'])){
                return outputJson(4000, '请选择采购方(甲方)');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入合同明细');
            }

            Db::startTrans();
            try {
                ContractCaiModel::update($param);


                ContractCaiListModel::where(['caiid'=>$param['id']])->delete();

                $isbudget = EnginBudgetModel::where(['deleted_user'=>0,'isturn'=>1,'status'=>1,'enginid'=>$old['enginid']])->find();
                $kknum = 1;
                foreach($list as $key=>$vv){
                    if($isbudget){
                        $bglist = EnginBudgetListModel::where(['id'=>$vv['listid']])->find();
                        $zhannum = ContractCaiListModel::alias('list')
                            ->join('contract_cai cai','cai.id = list.caiid')
                            ->where(['cai.deleted_user'=>0,'cai.isturn'=>1])
                            ->where('cai.status','in',[0,1])
                            ->where(['list.listid'=>$vv['listid']])
                            ->sum('list.num');
                        $shengnum = $bglist['num'] - $zhannum;
                        if($vv['num'] > $shengnum){
                            throw new Exception("第".$kknum."行采购数量已超预算剩余量");
                        }
                    }
                    $kknum++;
                }

                foreach($list as $vv){
                    $vv['caiid'] = $param['id'];
                    ContractCaiListModel::insert($vv);
                }


                (new OptService())->relation_file($files,'contract_cai',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_cai', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_cai', $param['id'], '采购合同');

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractCaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $data['data']['caier_info'] = (new UserService())->get_users_part_arr($data['data']['caier'], 'dept');

            $data['data']['budgetid'] = EnginBudgetModel::where(['enginid'=>$data['data']['enginid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])->value('id');

            $list = ContractCaiListModel::where(['caiid'=>$data['data']['id']])->select()->toArray();
            foreach ($list as &$item){
                $item['subid'] = 0;
                $item['guiname'] = '';
                $item['guiname'] = '';
                $listinfo = EnginBudgetListModel::where(['id'=>$item['listid']])->find();
                if($listinfo){
                    $item['subid'] = $listinfo['subid'];
                    $item['subname'] = (new BudgetService())->get_subid_quanname($listinfo['subid']);
                    $item['guiname'] = $listinfo['name'];
                }
            }
            $data['list'] = $list;
            $data['subarr'] = (new BudgetService())->get_budsubquan_all();
            $data['year'] = (new ContractService())->get_contract_yeararr();
            $data['state'] = (new ContractService())->get_contract_cai_state();
            $data['caihetype'] = (new ContractService())->get_contract_caihetype();
            $data['caiheshu'] = (new OptService())->get_base_info('engin_caiheshu');
            $data['files'] = (new OptService())->get_file('contract_cai', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function qian_contract_cai(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if($param['state'] == ''){
                return outputJson(4000, '请选择合同状态');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);
            Db::startTrans();
            try {
                ContractCaiModel::update($param);
                (new OptService())->relation_file($files,'contract_cai',$param['id']);
                (new OptService())->add_flow_log('签订', 'contract_cai', $param['id']);
                (new OptService())->add_log_list('签订', 'contract_cai', $param['id'], '内包协议');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractCaiModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['state'] = (new ContractService())->get_contract_cai_state();
            $data['files'] = (new OptService())->get_file('contract_cai', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_contract_cai(Request $request){
        $param = $request->post();
        $data = ContractCaiModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            ContractCaiModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'contract_cai', $data['id'], '采购合同');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_contract_cai(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = ContractCaiModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['data']['caihetype_name'] = (new ContractService())->get_contract_caihetype_name($data['data']['caihetype']);
        $data['data']['caiheshu'] = (new OptService())->get_base_showname('engin_caiheshu', $data['data']['caiheshu']);
        $data['data']['state_name'] = (new ContractService())->get_contract_cai_state_name($data['data']['id'], $data['data']['state']);
        $data['data']['caier_name'] = DeptModel::where(['id'=>$data['data']['caier']])->value('name');

        $is_budget = EnginBudgetModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1])
            ->where(['status'=>1,'enginid'=>$data['data']['enginid']])->find();
        $data['data']['is_budget'] = $is_budget ? true : false;

        $bian = ContractBianModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'caiid'=>$data['data']['id']])
            ->where('status','in',[0,1])->select()->toArray();
        $data['bian'] = $bian;

        $list = ContractCaiListModel::where(['caiid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['subname'] = '';
            $item['guiname'] = '';
            $item['chao_num'] = false;
            $item['chao_money'] = false;
            $listinfo = EnginBudgetListModel::where(['id'=>$item['listid']])->find();
            if($listinfo){
                $item['subname'] = (new BudgetService())->get_subid_quanname($listinfo['subid']);
                $item['guiname'] = $listinfo['name'];

                $allnum = ContractCaiListModel::alias('list')
                    ->join('contract_cai cai','cai.id=list.caiid')
                    ->where(['cai.comid'=>$this->userinfo['comid'],'cai.deleted_user'=>0,'isturn'=>1])
                    ->where('cai.status','in',[0,1])
                    ->where(['list.listid'=>$item['listid']])->sum('list.num');
                if($allnum > $listinfo['num']){
                    $item['chao_num'] = true;
                }

                $allmoney = ContractCaiListModel::alias('list')
                    ->join('contract_cai cai','cai.id=list.caiid')
                    ->where(['cai.comid'=>$this->userinfo['comid'],'cai.deleted_user'=>0,'isturn'=>1])
                    ->where('cai.status','in',[0,1])
                    ->where(['list.listid'=>$item['listid']])->sum('list.money');
                if($allmoney > $listinfo['money']){
                    $item['chao_money'] = true;
                }
            }

            $item['runum'] =  StuffRuListModel::alias('list')
                ->join('stuff_ru ru','ru.id=list.ruid')
                ->where(['ru.deleted_user'=>0,'ru.isturn'=>1])
                ->where('ru.status','in',[0,1])
                ->where('ru.ishe','=',1)
                ->where('list.listid','=',$item['id'])
                ->sum('list.num');
            $item['shengnum'] = round($item['num'] - $item['runum']);
        }
        $data['list'] = $list;



        $data['data']['lei_jie'] = StuffJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['enginid'=>$data['data']['enginid'],'caiid'=>$data['data']['id'],'status'=>1,'isturn'=>1])
            ->sum('money');
        $data['data']['lei_jie'] += LabourShenModel::alias('shen')
            ->where(['shen.comid'=>$this->userinfo['comid'],'shen.deleted_user'=>0])
            ->where(['shen.enginid'=>$data['data']['enginid'],'shen.caiid'=>$data['data']['id'],'shen.status'=>1,'shen.isturn'=>1])
            ->sum('shen.money');
        $data['data']['lei_jie'] += SubbaoShenModel::alias('shen')
            ->where(['shen.comid'=>$this->userinfo['comid'],'shen.deleted_user'=>0])
            ->where(['shen.enginid'=>$data['data']['enginid'],'shen.caiid'=>$data['data']['id'],'shen.status'=>1,'shen.isturn'=>1])
            ->sum('shen.money');
        $data['data']['lei_jie'] += DeviceJieModel::alias('shen')
            ->where(['shen.comid'=>$this->userinfo['comid'],'shen.deleted_user'=>0])
            ->where(['shen.enginid'=>$data['data']['enginid'],'shen.caiid'=>$data['data']['id'],'shen.status'=>1,'shen.isturn'=>1])
            ->sum('shen.money');


        $data['data']['lei_piaoshou'] = PiaoShouListModel::alias('list')
            ->join('piao_shou shou','shou.id=list.shouid')
            ->where(['shou.comid'=>$this->userinfo['comid'],'shou.deleted_user'=>0])
            ->where(['shou.enginid'=>$data['data']['enginid'],'list.caiid'=>$data['data']['id'],'shou.status'=>1,'shou.isturn'=>1])
            ->sum('list.money');


        $data['data']['lei_shenfu'] = PaymentEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['enginid'=>$data['data']['enginid'],'caiid'=>$data['data']['id'],'status'=>1,'isturn'=>1])
            ->sum('money');
        $paenid = PaymentEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
            ->where(['enginid'=>$data['data']['enginid'],'caiid'=>$data['data']['id'],'status'=>1,'isturn'=>1])
            ->column('id');
        $data['data']['lei_yifu'] =  AccountListModel::where(['type'=>3])->where('mid','in',$paenid)->sum('money');






        $data['files'] = (new OptService())->get_file('contract_cai', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function show_contract_cai_bglist(Request $request){
        $listid = $request->post('listid', 0, 'int');

        $list = [];
        $listinfo = EnginBudgetListModel::where(['id'=>$listid])->find();
        if(!$listinfo){
            return outputJson(2000,lang('code_2000'), $list);
        }
        $listinfo['subname'] = (new BudgetService())->get_subid_quanname($listinfo['subid']);
        $listinfo['ishetong'] = 0;
        $listinfo['jinname'] = '#预算项';
        $listinfo['list_num'] = 0;
        $listinfo['shengnum'] = 0;
        $listinfo['list_money'] = 0;
        $listinfo['avamoney'] = 0;
        $listinfo['shengmoney'] = 0;

        $cailist = ContractCaiListModel::alias('list')
            ->field('cai.id as hetongid,list.name,list.num as list_num,list.money as list_money,list.unit,list.price as avamoney')
            ->join('contract_cai cai','cai.id=list.caiid')
            ->where(['cai.comid'=>$this->userinfo['comid'],'cai.deleted_user'=>0,'isturn'=>1])
            ->where('cai.status','in',[0,1])
            ->where(['list.listid'=>$listid])->select()->toArray();
        foreach ($cailist as &$item){
            $item['ishetong'] = 1;
            $item['jinname'] = '#合同项';
            $item['shengnum'] = '/';
            $item['shengmoney'] = '/';
            $item['num'] = '/';
            $item['price'] = '/';
            $item['money'] = '/';
            $item['subname'] = $listinfo['subname'];
            $listinfo['list_num'] += $item['list_num'];
            $listinfo['list_money'] += $item['list_money'];
        }

        $listinfo['list_num'] = round($listinfo['list_num'], 3);
        $listinfo['list_money'] = round($listinfo['list_money'], 3);

        $listinfo['shengnum'] = round($listinfo['num'] - $listinfo['list_num'], 2);
        $listinfo['shengmoney'] = round($listinfo['money'] - $listinfo['list_money'], 2);
        if($listinfo['list_num'] != 0){
            $listinfo['avamoney'] = round($listinfo['list_money'] / $listinfo['list_num'], 2);
        }
        $list[] = $listinfo;
        foreach ($cailist as $vvvv){
            $list[] = $vvvv;
        }




        $data['data'] = $list;
        return outputJson(2000,lang('code_2000'), $data);
    }



    public function list_contract_bian(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['bian.deleted_user','=', 0];
        $where[] = ['bian.comid','=',$this->userinfo['comid']];
        $where[] = ['bian.isturn','=',1];
        $where[] = ['bian.status','in',[0,1]];
        $where[] = ['bian.caiid','=',$param['caiid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bian.number|cai.name|cai.number|bian.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['bian.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['cainumber']) && !empty($param['cainumber'])){
            $where[] = ['cai.number','like', '%'.$param['cainumber'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['bian.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['bian.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['bian.status','=', $param['status']];
        }

        $data['data'] = ContractBianModel::alias('bian')
            ->field('bian.*,e.name as enginname,cai.name as cainame,cai.number as cainumber')
            ->LeftJoin('contract_cai cai','cai.id=bian.caiid')
            ->LeftJoin('engin e','e.id=cai.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_bian', 'bian'))
            ->order('bian.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        $data['caiinfo'] = ContractCaiModel::where(['id'=>$param['caiid']])->find();
        $data['butauth'] = (new MenuService())->get_butauth('contract_bian');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_contract_bian(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['dt'])){
                return outputJson(4000, '请选择变更日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经办人');
            }


            $cai_money = ContractCaiModel::where(['id'=>$param['caiid']])->value('money');
            $bian_money = $cai_money + $param['bianfee'];

            $param['yuanfee'] = $cai_money;
            $param['number'] = (new ContractService())->create_contract_bian_number();
            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['isturn'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入变更明细');
            }


            Db::startTrans();
            try {
                $id = ContractBianModel::insertGetId($param);
                ContractCaiModel::where(['id'=>$param['caiid']])->update(['money'=>$bian_money]);

                foreach($list as $vv){
                    $vv['bianid'] = $id;
                    ContractBianListModel::insert($vv);

                    ContractCaiListModel::where(['id'=>$vv['listid']])->update([
                        'num' => $vv['num'],
                        'price' => $vv['price'],
                        'money' => $vv['money'],
                    ]);
                }

                (new OptService())->relation_file($files,'contract_bian',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_bian', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_bian', $id, '合同变更');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $caiid = $request->param('caiid', 0, 'int');
            $caiinfo = ContractCaiModel::where(['id'=>$caiid])->find();
            $caiinfo['enginid_name'] = EnginModel::where(['id'=>$caiinfo['enginid']])->value('name');
            $data['caiinfo'] = $caiinfo;
            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['number'] = (new ContractService())->create_contract_bian_number();
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_contract_bian(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = ContractBianModel::where(['id'=>$param['id']])->find();

            if(empty($param['dt'])){
                return outputJson(4000, '请选择变更日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经办人');
            }


            $cai_money = ContractCaiModel::where(['id'=>$old['caiid']])->value('money');
            $bian_money = $cai_money + $param['bianfee'];


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入变更明细');
            }

            Db::startTrans();
            try {
                ContractBianModel::update($param);
                ContractCaiModel::where(['id'=>$old['caiid']])->update(['money'=>$bian_money]);


                ContractBianListModel::where(['bianid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['bianid'] = $param['id'];
                    ContractBianListModel::insert($vv);


                    ContractCaiListModel::where(['id'=>$vv['listid']])->update([
                        'num' => $vv['num'],
                        'price' => $vv['price'],
                        'money' => $vv['money'],
                    ]);
                }

                (new OptService())->relation_file($files,'contract_bian',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_bian', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_bian', $param['id'], '合同变更');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractBianModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $caiinfo = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();
            $caiinfo['enginid_name'] = EnginModel::where(['id'=>$caiinfo['enginid']])->value('name');
            $data['caiinfo'] = $caiinfo;


            $list = ContractBianListModel::where(['bianid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('contract_bian', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_contract_bian(Request $request){
        $param = $request->post();
        $data = ContractBianModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            ContractBianModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'contract_bian', $data['id'], '合同变更');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_contract_bian(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = ContractBianModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $caiinfo = ContractCaiModel::where(['id'=>$data['data']['caiid']])->find();
        $caiinfo['enginid_name'] = EnginModel::where(['id'=>$caiinfo['enginid']])->value('name');
        $data['caiinfo'] = $caiinfo;


        $list = ContractBianListModel::where(['bianid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['subname'] = '';
            $item['guiname'] = '';
            $listinfo = EnginBudgetListModel::where(['id'=>$item['subid']])->find();
            if($listinfo){
                $item['subname'] = (new BudgetService())->get_subid_quanname($listinfo['subid']);
                $item['guiname'] = $listinfo['name'];
            }

            $item['lai'] = $item['listid'] > 0 ? '合同' : '新增';
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('contract_bian', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }




    public function list_contract_kuan(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['kuan.deleted_user','=', 0];
        $where[] = ['kuan.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }

        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
        $gckshou = (new BudgetService())->get_budshou_sons($gckid);

        $data['data'] = ContractKuanModel::alias('kuan')
            ->field('kuan.*,e.name as enginname,shi.name as shi_name,shi.number as shi_number,shi.money as shi_money')
            ->join('engin e','e.id=kuan.enginid')
            ->join('contract_shi shi','shi.id=kuan.shiid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_kuan', 'kuan'))
            ->order('kuan.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($gckshou){
                $item['fee_dang'] = ContractKuanListModel::where(['kuanid'=>$item['id']])->where('dt','elt',date("Y-m-d"))->sum('money');
                $item['fee_shi'] = IncomeEnginModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['enginid'],'deleted_user'=>0])
                    ->where('budid','in',$gckshou)->sum('money');
                $item['shi_money'] = (new EnginService())->get_contract_shi_final_money($item['enginid']);
                $item['fee_sheng'] = round($item['fee_dang'] - $item['fee_shi'], 2);

                $item['hui_dt'] = '';
                $item['hui_rate'] = '';
                $item['hui_money'] = '';
                $hui = ContractKuanListModel::where(['kuanid'=>$item['id']])
                    ->where('dt','gt',date("Y-m-d"))->order('dt ASC')->find();
                if($hui){
                    $item['hui_dt'] = $hui['dt'];
                    $item['hui_rate'] = $hui['rate'];
                    $item['hui_money'] = $hui['money'];
                }
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('contract_kuan');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_contract_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['enginid'])){
                return outputJson(4000, '请选择项目名称');
            }
            if(empty($param['shiid'])){
                return outputJson(4000, '请选择合同名称');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入回款计划');
            }

            Db::startTrans();
            try {
                $id = ContractKuanModel::insertGetId($param);

                foreach($list as $v){
                    $v['kuanid'] = $id;
                    ContractKuanListModel::insert($v);
                }

                (new OptService())->relation_file($files,'contract_kuan',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'contract_kuan', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'contract_kuan', $id, '回款计划');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            return outputJson(2000,lang('code_2000'));
        }
    }
    public function edit_contract_kuan(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $old = ContractKuanModel::where(['id'=>$param['id']])->find();


            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            unset($param['token']);

            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) < 1){
                return outputJson(4000, '请输入回款计划');
            }

            Db::startTrans();
            try {
                ContractKuanModel::update($param);

                ContractKuanListModel::where(['kuanid'=>$param['id']])->delete();
                foreach($list as $v){
                    $v['kuanid'] = $param['id'];
                    ContractKuanListModel::insert($v);
                }

                (new OptService())->relation_file($files,'contract_kuan',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'contract_kuan', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'contract_kuan', $param['id'], '回款计划');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = ContractKuanModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $engininfo = EnginModel::get_info_by_pk($data['data']['enginid']);
            $data['engininfo'] = $engininfo;

            $shiinfo = ContractShiModel::get_info_by_pk($data['data']['shiid']);
            $data['shiinfo'] = $shiinfo;

            $list = ContractKuanListModel::where(['kuanid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;

            $data['files'] = (new OptService())->get_file('contract_kuan', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_contract_kuan(Request $request){
        $param = $request->post();
        $data = ContractKuanModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            ContractKuanModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'contract_kuan', $data['id'], '回款计划');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_contract_kuan(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = ContractKuanModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $data['shiinfo'] = ContractShiModel::get_info_by_pk($data['data']['shiid']);

        $list = ContractKuanListModel::where(['kuanid'=>$data['data']['id']])->select()->toArray();
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('contract_kuan', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }



    public function list_contract_shi_tai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['shi.deleted_user','=', 0];
        $where[] = ['shi.comid','=',$this->userinfo['comid']];
        $where[] = ['shi.isturn','=',1];
        $where[] = ['shi.status','=',1];


        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['shiname']) && !empty($param['shiname'])){
            $where[] = ['shi.name','like', '%'.$param['shiname'].'%'];
        }
        if(isset($param['faer']) && !empty($param['faer'])){
            $where[] = ['shi.faer','like', '%'.$param['faer'].'%'];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
        $budshou = (new BudgetService())->get_budshou_sons($gckid);

        $data['data'] = ContractShiModel::alias('shi')
            ->field('shi.*,e.state as engin_state,e.chanid as engin_chanid,e.business,e.name as enginname')
            ->leftJoin('engin e','e.id=shi.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_shi_tai', 'e'))
            ->order('shi.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($budshou){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['enginid'], $item['engin_state']);
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['engin_chanid']);

                $item['shimoney'] = (new EnginService())->get_contract_shi_final_money($item['enginid']);

                $item['lei_chan'] = EnginChanModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['enginid'],'status'=>1,'isturn'=>1])->sum('leijin');

                $item['lei_piaokai'] = PiaokaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['enginid'],'status'=>1,'isturn'=>1])
                    ->where("dt is not null and dt != 0000-00-00 and dt != ''")->sum('money');

                $item['lei_huikuan'] = IncomeEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['enginid']])->where('budid','in',$budshou)->sum('money');

                $item['lei_daihui'] = $item['lei_chan'] - $item['lei_huikuan'];

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_contract_cai_tai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['cai.deleted_user','=', 0];
        $where[] = ['cai.comid','=',$this->userinfo['comid']];
        $where[] = ['cai.isturn','=',1];
        $where[] = ['cai.status','=',1];


        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['cainame']) && !empty($param['cainame'])){
            $where[] = ['cai.name','like', '%'.$param['cainame'].'%'];
        }
        if(isset($param['caihetype']) && !empty($param['caihetype'])){
            $where[] = ['cai.caihetype','=', $param['caihetype']];
        }
        if(isset($param['gonger']) && !empty($param['gonger'])){
            $where[] = ['cai.gonger','like', '%'.$param['gonger'].'%'];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';

        $data['caihetype'] = (new ContractService())->get_contract_caihetype();
        $data['is_caihetype'] = isset($param['caihetype']) ? $param['caihetype'] : '';

        $data['data'] = ContractCaiModel::alias('cai')
            ->field('cai.*,e.state as engin_state,e.chanid as engin_chanid,e.business,e.name as enginname')
            ->leftJoin('engin e','e.id=cai.enginid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_cai_tai', 'e'))
            ->order('cai.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['caihetype_name'] = (new ContractService())->get_contract_caihetype_name($item['caihetype']);
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['enginid'], $item['engin_state']);
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['engin_chanid']);

                $item['lei_jie'] = StuffJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['caiid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');
                $item['lei_jie'] += LabourShenModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['caiid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');
                $item['lei_jie'] += SubbaoShenModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['caiid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');
                $item['lei_jie'] += DeviceJieModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['caiid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');

                $item['lei_piaoshou'] = PiaoShouListModel::alias('list')
                    ->join('piao_shou zhu','zhu.id=list.shouid')
                    ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
                    ->where(['list.caiid'=>$item['id']])
                    ->sum('list.money');

                $item['lei_fukuan'] = PaymentEnginModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['caiid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');
                $item['lei_daifu'] = $item['lei_jie'] - $item['lei_fukuan'];

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }



    public function list_contract_tai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];


        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_tai', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);

                $item['shiinfo'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0])
                    ->where(['status'=>1,'isturn'=>1])->find();
                $item['shimoney'] = (new EnginService())->get_contract_shi_final_money($item['id']);

                $item['cainum'] = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->count();

                $item['caimoney'] = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');

                $item['cha'] = $item['shimoney'] - $item['caimoney'];

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }



    public function list_contract_nei_tai(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.isturn','=',1];
        $where[] = ['e.status','=',1];


        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['state']) && $param['state'] != ''){
            $where[] = ['e.state','=', $param['state']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $state = (new EnginService())->get_engin_state();
        $data['state'] = $state;
        $data['is_state'] = isset($param['state']) ? $param['state'] : '';


        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('contract_tai', 'e'))
            ->order('e.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);

                $item['shiinfo'] = ContractShiModel::where(['comid'=>$this->userinfo['comid'],'enginid'=>$item['id'],'deleted_user'=>0])
                    ->where(['status'=>1,'isturn'=>1])->find();
                $item['shimoney'] = (new EnginService())->get_contract_shi_final_money($item['id']);

                $item['cainum'] = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->count();

                $item['caimoney'] = ContractCaiModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                    ->where(['enginid'=>$item['id'],'status'=>1,'isturn'=>1])->sum('money');

                $item['cha'] = $item['shimoney'] - $item['caimoney'];

                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }



}
